package fun.abbas.myapplication;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.zx.sercurity.AsyncResultWrapper;
import com.zx.sercurity.AsyncRiskListener;
import com.zx.sercurity.RiskControlManager;
import com.zx.sercurity.RiskEnum;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

import fun.abbas.myapplication.databinding.ActivityLifecycleTestBinding;

public class LifecycleTestActivity extends AppCompatActivity implements AsyncRiskListener {

    private fun.abbas.myapplication.databinding.ActivityLifecycleTestBinding binding;
    private final MyAdapter adapter = new MyAdapter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityLifecycleTestBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(binding.appbar, (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        binding.recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        binding.recycler.setAdapter(adapter);

        RiskControlManager.getInstance().addAsyncRiskCheckListener(this);
        RiskControlManager.getInstance().startAsyncRiskCheck(this, getLifecycle());

    }

    private final HashMap<RiskEnum, AsyncResultWrapper<?>> dataMap = new HashMap<>();

    @Override
    public void onAsyncRiskStateChange(AsyncResultWrapper<?> asyncResultWrapper) {
        dataMap.put(asyncResultWrapper.getRiskEnum(), asyncResultWrapper);
        adapter.submitList(new ArrayList<>(dataMap.values()));
    }

    @Override
    public void onFindAsyncRisk(AsyncResultWrapper<?> asyncResultWrapper) {
        dataMap.put(asyncResultWrapper.getRiskEnum(), asyncResultWrapper);
        adapter.submitList(new ArrayList<>(dataMap.values()));
    }
}